/*
 * Copyright (c) 2024 Huawei Technologies Co.,Ltd.
 *
 * openGauss is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 * http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 * -------------------------------------------------------------------------
 *
 * htap_cfg.cpp
 * Holds global configuration for the HTAP module.
 *
 * IDENTIFICATION
 * src/gausskernel/storage/htap/htap_cfg.cpp
 *
 * -------------------------------------------------------------------------
 */

#include "htap_cfg.h"
#include "htap_logger.h"
#include "config_manager.h"
#include "log_format_formatter.h"

namespace htap {
HTAP_DECLARE_LOGGER(HTAPCfg, HTAP)

constexpr bool HTAPCfg::DEFAULT_ENABLE_HTAP;
constexpr MOT::LogFormat HTAPCfg::DEFAULT_LOG_FORMAT;
constexpr bool HTAPCfg::DEFAULT_ENABLE_STATS;
constexpr const char *HTAPCfg::DEFAULT_STATS_PRINT_PERIOD;
constexpr uint64_t HTAPCfg::DEFAULT_STATS_PRINT_PERIOD_SECONDS;
constexpr uint64_t HTAPCfg::MIN_STATS_PRINT_PERIOD_SECONDS;
constexpr uint64_t HTAPCfg::MAX_STATS_PRINT_PERIOD_SECONDS;
constexpr const char *HTAPCfg::DEFAULT_FULL_STATS_PRINT_PERIOD;
constexpr uint64_t HTAPCfg::DEFAULT_FULL_STATS_PRINT_PERIOD_SECONDS;
constexpr bool HTAPCfg::DEFAULT_ENABLE_PROCESS_STAT_PRINT;
constexpr bool HTAPCfg::DEFAULT_ENABLE_SYSTEM_STAT_PRINT;
constexpr bool HTAPCfg::DEFAULT_ENABLE_HOOK_STAT_PRINT;
constexpr bool HTAPCfg::DEFAULT_ENABLE_DELTA_STAT_PRINT;
constexpr bool HTAPCfg::DEFAULT_ENABLE_FLUSH_STAT_PRINT;
constexpr bool HTAPCfg::DEFAULT_ENABLE_SEGMENT_TREE;
constexpr bool HTAPCfg::DEFAULT_ENABLE_FLUSH;
constexpr bool HTAPCfg::DEFAULT_ENABLE_HTAP_XLOG_REDOER;
constexpr uint64_t HTAPCfg::DEFAULT_FLUSH_TIMEOUT_MILLIS;
constexpr bool HTAPCfg::DEFAULT_ENABLE_FLUSH_PRINT_STATS;
constexpr uint64_t HTAPCfg::DEFAULT_HTAP_XLOG_REDOER_TIMEOUT_MILLIS;

HTAPCfg::HTAPCfg()
    : m_enableHtap(DEFAULT_ENABLE_HTAP),
      m_logFormat(DEFAULT_LOG_FORMAT),
      m_enableStats(DEFAULT_ENABLE_STATS),
      m_statPrintPeriodSeconds(DEFAULT_STATS_PRINT_PERIOD_SECONDS),
      m_statPrintFullPeriodSeconds(DEFAULT_FULL_STATS_PRINT_PERIOD_SECONDS),
      m_enableProcessStatistics(DEFAULT_ENABLE_PROCESS_STAT_PRINT),
      m_enableSystemStatistics(DEFAULT_ENABLE_SYSTEM_STAT_PRINT),
      m_enableModifyStatistics(DEFAULT_ENABLE_HOOK_STAT_PRINT),
      m_enableDeltaStatistics(DEFAULT_ENABLE_DELTA_STAT_PRINT),
      m_enableFlushStatistics(DEFAULT_ENABLE_FLUSH_STAT_PRINT),
      m_enableSegmentTree(DEFAULT_ENABLE_SEGMENT_TREE),
      m_enableFlush(DEFAULT_ENABLE_FLUSH),
      m_flushTimeoutMillis(DEFAULT_FLUSH_TIMEOUT_MILLIS),
      m_enabelHTAPXLogRedoer(DEFAULT_ENABLE_HTAP_XLOG_REDOER),
      m_htapXLogRedoTimeoutMillis(DEFAULT_HTAP_XLOG_REDOER_TIMEOUT_MILLIS)
{}

void HTAPCfg::OnConfigChange()
{
    HTAP_LOG_TRACE("Reloading HTAP configuration after change");
    LoadConfig();
}

void HTAPCfg::LoadConfig()
{
    ConfigureMOTEngine();
    SetTotalMemoryMb(MOT::GetGlobalConfiguration().m_totalMemoryMb);
    const MOT::LayeredConfigTree *cfg = MOT::ConfigManager::GetInstance().GetLayeredConfigTree();

    // load component log levels & log format
    UpdateComponentLogLevel();
    UPDATE_USER_CFG(m_logFormat, "log_format", DEFAULT_LOG_FORMAT);

    // now load all other configuration items
    HTAP_LOG_TRACE("Loading HTAP main configuration");

    // statistics configuration
    UPDATE_BOOL_CFG(m_enableStats, "enable_stats", DEFAULT_ENABLE_STATS);
    UPDATE_TIME_CFG(m_statPrintPeriodSeconds, "print_stats_period", DEFAULT_STATS_PRINT_PERIOD, SCALE_SECONDS,
        MIN_STATS_PRINT_PERIOD_SECONDS, MAX_STATS_PRINT_PERIOD_SECONDS);
    UPDATE_TIME_CFG(m_statPrintFullPeriodSeconds, "print_full_stats_period", DEFAULT_FULL_STATS_PRINT_PERIOD,
        SCALE_SECONDS, MIN_STATS_PRINT_PERIOD_SECONDS, MAX_STATS_PRINT_PERIOD_SECONDS);
    UPDATE_BOOL_CFG(m_enableProcessStatistics, "enable_process_stats", DEFAULT_ENABLE_PROCESS_STAT_PRINT);
    UPDATE_BOOL_CFG(m_enableSystemStatistics, "enable_system_stats", DEFAULT_ENABLE_SYSTEM_STAT_PRINT);
    UPDATE_BOOL_CFG(m_enableModifyStatistics, "enable_hook_stats", DEFAULT_ENABLE_HOOK_STAT_PRINT);
    UPDATE_BOOL_CFG(m_enableDeltaStatistics, "enable_delta_stats", DEFAULT_ENABLE_DELTA_STAT_PRINT);
    UPDATE_BOOL_CFG(m_enableFlushStatistics, "enable_flush_stats", DEFAULT_ENABLE_FLUSH_STAT_PRINT);
    UPDATE_BOOL_CFG(m_enableSegmentTree, "enable_segment_tree", DEFAULT_ENABLE_SEGMENT_TREE);
    UPDATE_BOOL_CFG(m_enableFlush, "enable_flush", DEFAULT_ENABLE_FLUSH);
    UPDATE_BOOL_CFG(m_enableFlushPrintStats, "enable_flush_print_stats", DEFAULT_ENABLE_FLUSH_PRINT_STATS);
    UPDATE_BOOL_CFG(m_enabelHTAPXLogRedoer, "enable_htap_xlog_redoer", DEFAULT_ENABLE_HTAP_XLOG_REDOER);

    UPDATE_TIME_CFG(m_flushTimeoutMillis, "flush_timeout", DEFAULT_FLUSH_TIMEOUT, SCALE_MILLIS,
        MIN_FLUSH_TIMEOUT_MILLIS, MAX_FLUSH_TIMEOUT_MILLIS);

    UPDATE_TIME_CFG(m_htapXLogRedoTimeoutMillis, "htap_xlog_redoer_timeout", DEFAULT_HTAP_XLOG_REDOER_TIMEOUT,
        SCALE_MILLIS, MIN_FLUSH_TIMEOUT_MILLIS, MAX_FLUSH_TIMEOUT_MILLIS);

    HTAP_LOG_TRACE("Main configuration loaded");
}

void HTAPCfg::ConfigureMOTEngine()
{
    MOT::MOTConfiguration &motCfg = MOT::GetGlobalConfiguration();
    htap::SetGlobalLogLevel(motCfg.m_logLevel);
    MOT::SetGlobalLogLevel(motCfg.m_logLevel);
    motCfg.m_enableRedoLog = false;
    motCfg.m_enableGroupCommit = false;
    motCfg.m_enableCheckpoint = false;
    motCfg.m_enableCodegen = false;
    motCfg.m_sessionAffinityMode = MOT::AffinityMode::AFFINITY_NONE;
}
} // namespace htap